package com.itheima.controller;

import com.itheima.pojo.Category;
import com.itheima.pojo.Result;
import com.itheima.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

	@Autowired
	private CategoryService categoryService;

	//添加类别
	@PostMapping
	public Result add(@RequestBody @Validated(Category.Add.class) Category category) {
		categoryService.add(category);
		return Result.success();
	}

	//查询列表
	@GetMapping
	public Result<List<Category>> list() {
		List<Category> cs = categoryService.list();
		return Result.success(cs);
	}

	//获取文章分类详情
	@GetMapping("/detail")
	public Result<Category> detail(Integer id) {
		Category category = categoryService.findById(id);
		return Result.success(category);
	}

	//更新文章分类
	@PutMapping
	public Result update(@RequestBody @Validated(Category.Update.class) Category category) {
		categoryService.update(category);
		return Result.success();
	}

	//根据id删除类别
	@DeleteMapping
	private Result deleteById(Integer id){
		boolean flat = categoryService.deleteById(id);
		if (flat){
			return Result.success("数据删除成功");
		}
		return Result.error("id未传递或数据库中不存在该类别");
	}
}
